﻿// Decompiled with JetBrains decompiler
// Type: MCM.Abstractions.Base.Global.SettingsWrapper
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using HarmonyLib.BUTR.Extensions;
using MCM.Common;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;

#nullable enable
namespace MCM.Abstractions.Base.Global
{
  public abstract class SettingsWrapper : BaseSettings, IWrapper
  {
    private readonly SettingsWrapper.GetIdDelegate? _getIdDelegate;
    private readonly SettingsWrapper.GetFolderNameDelegate? _getFolderNameDelegate;
    private readonly SettingsWrapper.GetDisplayNameDelegate? _getDisplayNameDelegate;
    private readonly SettingsWrapper.GetUIVersionDelegate? _getUIVersionDelegate;
    private readonly SettingsWrapper.GetSubFolderDelegate? _getSubFolderDelegate;
    private readonly SettingsWrapper.GetSubGroupDelimiterDelegate? _getSubGroupDelimiterDelegate;
    private readonly SettingsWrapper.GetFormatDelegate? _getFormatDelegate;
    private readonly SettingsWrapper.OnPropertyChangedDelegate? _methodOnPropertyChangedDelegate;
    private readonly SettingsWrapper.GetBuiltInPresetsDelegate? _methodGetBuiltInPresetsDelegate;
    private readonly SettingsWrapper.CreateNewDelegate? _methodCreateNewDelegate;
    private readonly SettingsWrapper.CopyAsNewDelegate? _methodCopyAsNewDelegate;

    public override string Id
    {
      get
      {
        SettingsWrapper.GetIdDelegate getIdDelegate = this._getIdDelegate;
        return (getIdDelegate != null ? getIdDelegate() : (string) null) ?? "ERROR";
      }
    }

    public override string FolderName
    {
      get
      {
        SettingsWrapper.GetFolderNameDelegate folderNameDelegate = this._getFolderNameDelegate;
        return (folderNameDelegate != null ? folderNameDelegate() : (string) null) ?? string.Empty;
      }
    }

    public override string DisplayName
    {
      get
      {
        SettingsWrapper.GetDisplayNameDelegate displayNameDelegate = this._getDisplayNameDelegate;
        return (displayNameDelegate != null ? displayNameDelegate() : (string) null) ?? "ERROR";
      }
    }

    public override int UIVersion
    {
      get
      {
        SettingsWrapper.GetUIVersionDelegate uiVersionDelegate = this._getUIVersionDelegate;
        return uiVersionDelegate == null ? 1 : uiVersionDelegate();
      }
    }

    public override string SubFolder
    {
      get
      {
        SettingsWrapper.GetSubFolderDelegate subFolderDelegate = this._getSubFolderDelegate;
        return (subFolderDelegate != null ? subFolderDelegate() : (string) null) ?? string.Empty;
      }
    }

    public override char SubGroupDelimiter
    {
      get
      {
        SettingsWrapper.GetSubGroupDelimiterDelegate delimiterDelegate = this._getSubGroupDelimiterDelegate;
        return delimiterDelegate == null ? '/' : delimiterDelegate();
      }
    }

    public override string FormatType
    {
      get
      {
        SettingsWrapper.GetFormatDelegate getFormatDelegate = this._getFormatDelegate;
        return (getFormatDelegate != null ? getFormatDelegate() : (string) null) ?? "none";
      }
    }

    public override event PropertyChangedEventHandler? PropertyChanged
    {
      add
      {
        if (!(this.Object is INotifyPropertyChanged notifyPropertyChanged))
          return;
        notifyPropertyChanged.PropertyChanged += value;
      }
      remove
      {
        if (!(this.Object is INotifyPropertyChanged notifyPropertyChanged))
          return;
        notifyPropertyChanged.PropertyChanged -= value;
      }
    }

    public object? Object { get; }

    protected SettingsWrapper(object? @object)
    {
      if (@object == null)
        return;
      this.Object = @object;
      Type type = @object.GetType();
      this._getIdDelegate = AccessTools2.GetPropertyGetterDelegate<SettingsWrapper.GetIdDelegate>(@object, type, nameof (Id));
      this._getFolderNameDelegate = AccessTools2.GetPropertyGetterDelegate<SettingsWrapper.GetFolderNameDelegate>(@object, type, nameof (FolderName));
      this._getDisplayNameDelegate = AccessTools2.GetPropertyGetterDelegate<SettingsWrapper.GetDisplayNameDelegate>(@object, type, nameof (DisplayName));
      this._getUIVersionDelegate = AccessTools2.GetPropertyGetterDelegate<SettingsWrapper.GetUIVersionDelegate>(@object, type, nameof (UIVersion));
      this._getSubFolderDelegate = AccessTools2.GetPropertyGetterDelegate<SettingsWrapper.GetSubFolderDelegate>(@object, type, nameof (SubFolder));
      this._getSubGroupDelimiterDelegate = AccessTools2.GetPropertyGetterDelegate<SettingsWrapper.GetSubGroupDelimiterDelegate>(@object, type, nameof (SubGroupDelimiter));
      this._getFormatDelegate = AccessTools2.GetPropertyGetterDelegate<SettingsWrapper.GetFormatDelegate>(@object, type, nameof (FormatType));
      this._methodOnPropertyChangedDelegate = AccessTools2.GetDelegate<SettingsWrapper.OnPropertyChangedDelegate>(@object, type, "OnPropertyChanged");
      this._methodGetBuiltInPresetsDelegate = AccessTools2.GetDelegate<SettingsWrapper.GetBuiltInPresetsDelegate>(@object, type, "GetBuiltInPresets");
      this._methodCreateNewDelegate = AccessTools2.GetDelegate<SettingsWrapper.CreateNewDelegate>(@object, type, "CreateNew");
      this._methodCopyAsNewDelegate = AccessTools2.GetDelegate<SettingsWrapper.CopyAsNewDelegate>(@object, type, "CopyAsNew");
    }

    protected abstract BaseSettings Create(object? @object);

    protected abstract ISettingsPreset CreatePreset(object? @object);

    public override void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
      SettingsWrapper.OnPropertyChangedDelegate propertyChangedDelegate = this._methodOnPropertyChangedDelegate;
      if (propertyChangedDelegate == null)
        return;
      propertyChangedDelegate(propertyName);
    }

    public override IEnumerable<ISettingsPreset> GetBuiltInPresets()
    {
      SettingsWrapper.GetBuiltInPresetsDelegate inPresetsDelegate = this._methodGetBuiltInPresetsDelegate;
      IEnumerable<ISettingsPreset> settingsPresets;
      if (inPresetsDelegate == null)
      {
        settingsPresets = (IEnumerable<ISettingsPreset>) null;
      }
      else
      {
        IEnumerable source = inPresetsDelegate();
        settingsPresets = source != null ? source.Cast<object>().Select<object, ISettingsPreset>(new Func<object, ISettingsPreset>(this.CreatePreset)).OfType<ISettingsPreset>() : (IEnumerable<ISettingsPreset>) null;
      }
      return settingsPresets ?? (IEnumerable<ISettingsPreset>) Array.Empty<ISettingsPreset>();
    }

    public override BaseSettings CreateNew()
    {
      SettingsWrapper.CreateNewDelegate createNewDelegate = this._methodCreateNewDelegate;
      return this.Create(createNewDelegate != null ? createNewDelegate() : (object) null);
    }

    public override BaseSettings CopyAsNew()
    {
      SettingsWrapper.CopyAsNewDelegate copyAsNewDelegate = this._methodCopyAsNewDelegate;
      return this.Create(copyAsNewDelegate != null ? copyAsNewDelegate() : (object) null);
    }

    private delegate string GetIdDelegate();

    private delegate string GetFolderNameDelegate();

    private delegate string GetDisplayNameDelegate();

    private delegate int GetUIVersionDelegate();

    private delegate string GetSubFolderDelegate();

    private delegate char GetSubGroupDelimiterDelegate();

    private delegate string GetFormatDelegate();

    private delegate void OnPropertyChangedDelegate(string? propertyName);

    private delegate IEnumerable? GetBuiltInPresetsDelegate();

    private delegate object CreateNewDelegate();

    private delegate object CopyAsNewDelegate();
  }
}
